<?php

if (!isset($_POST['title']) || !isset($_POST['content'])) {
    echo 'Invalid post';
    return;
}

$title = $_POST['title'];
$content = $_POST['content'];

session_start();
$author = $_SESSION['username'];
$author_id = $_SESSION['uid'];
session_write_close();

$mysqli = new mysqli('localhost', 'blog-admin', 'blog-admin', 'blog');
if ($mysqli->connect_errno) {
    echo 'Failed to connect to MySQL: (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error;
    return;
}

$sql = 'INSERT INTO articles(title, content, author_id) VALUES("' . $title . '", "' . base64_encode($content) . '",' . $author_id . ')';
if (!$mysqli->query($sql)) {
    echo 'Failed to insert into mysql: ' . $mysqli->error;
    return;
}

$mysqli->close();

echo <<<eof
  <script type='text/javascript'>
    window.location = '/post.php';
  </script>
eof;

?>